<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <?php require APP_PATH_WEB_ANLS.'Report/common/web_doc_meta.php';?>
    <title>配置</title>
    <?php require APP_PATH_WEB_ANLS.'Report/common/web_doc_link.php';?>
    <?php require APP_PATH_WEB_ANLS.'Report/common/web_doc_script.php';?>
    <script src="/Public/js/echarts.common.min.js"></script>
	<style>
	    #log {width:500px; height: 500px;}
	    
	    .divTop{ font-size:14px; float:left; height:120px; z-index:99999; margin-top:10px;}
	        .divTop input { margin:2px;cursor: pointer; }
			.divTop .btnList { overflow-y:scroll; float:left; width:480px;height:115px;}
			.divTop .searchCode {width:638px;}
		.divSearch{font-size:14px; float: left;  margin-top:10px;}
			.input_bg_red {background-color: red;}
		.divChart{ float: left;}
		.input_bg_yellow {background-color: yellow;}
    </style>
    <script type="text/javascript">

    var anlsData = null;
    
	function setTotalChart(chartData,xData){
		var total = echarts.init(document.getElementById('total'));
		var option = {
				animation:false,
				legend: {
			        data:['总盈利']
			    },
				grid: {
			        left: '0%',
			        right: '0%',
			        top: '10%',
			        bottom: '3%',
			        containLabel: true
			    },
			    title: {
			        text: '总盈利'
			    },
			    tooltip: {trigger: 'axis'},
			    xAxis: {
		        	data:xData
			    },
			    yAxis: {
			    	interval : 5,
			        type: 'value'
			    },
	            
			    series: [{
			        name: '总盈利',
			        type: 'line',
			        showAllSymbol: true,
			        hoverAnimation: false,
			        data: chartData
			    }
			    
			    ]
			};
		total.setOption(option);
	}

	function setBmhpChart(chartData,lostData,bmcpData,xData,bmhpTimesChartData){
		var bmhp = echarts.init(document.getElementById('bmhp'));
		var option = {
				animation:false,
			    title: {
			        text: $("#code").val()+'分析'
			    },
			    tooltip: {trigger: 'axis'},
			    
			    grid: {
			        left: '0%',
			        right: '0%',
			        top: '10%',
			        bottom: '3%',
			        containLabel: true
			    },
			    legend: {
			        data:['最高盈利','最高盈利少于5次数','连赢','平仓盈利']
			    },
			    xAxis: {
		        	data:xData
			    },
			    yAxis: {
			    	interval : 5,
			        type: 'value'
			    },
			    color:[ '#c23531', '#749f83',  '#ca8622', '#bda29a', '#c4ccd3']
			    ,series: [
					{
				        name: '最高盈利',
				        type: 'line',
				        showAllSymbol : true,
				        hoverAnimation: false,
				        data: chartData
				    },
				    {
				        name: '最高盈利少于5次数',
				        type: 'line',
				        showAllSymbol: true,
				        hoverAnimation: false,
				        data: bmhpTimesChartData
				    },
				    {
    			        name: '连赢',
    			        type: 'line',
    			        showAllSymbol: true,
    			        hoverAnimation: false,
    			        data: lostData
    			    },{
    			        name: '平仓盈利',
    			        type: 'line',
    			        showAllSymbol : true,
    			        hoverAnimation: false,
    			        lineStyle:{
        			        width:1,
							normal : {opacity:0.2}
        			    },
    			        data: bmcpData
    			    }
			    ]
			};
			bmhp.setOption(option);
	}

	function init(code){
			$.ajax({
				url:'/Anls/trade/getTradeLog?code='+code
				,dataType:'json'
				,success:function(data){
					if(!data && !data.length) return false;
					var bmhpChartData = [];
					var bmcpChartData = [];
					var lostChartData = [];
					var totalChartData = [];
					var bmhpTimesChartData = [];
					var xData = [];
					var total = 0;

					// 连输次数
					var lost = 0;
					// 最高盈利<N次数
					var bmhpTimes = 0;
					for(var i=0;i<data.length;i++){
						var log = JSON.parse(data[i]);
						//console.info(log.bwp);
						var date = log.buyCloseDate;
						dateNum = parseInt(date.replace(/\//g,""));
						var inputDate = $("#inputDate").val()+'0000';
						
						if(dateNum<parseInt(inputDate)) continue;
						date = date.substring(2,date.length);
						date = date.replace(/\//g,"-");
						xData.push(date);
						total = total + log.bwp;
						//total = total.toFixed(2);
						var bmhp = log.bmhp > 100? 100:log.bmhp;
						if(log.buyWinPoint <0){
							if(lost>0){
								lost = -1;
							} else {
								lost--;
							}
						} else {
							if(lost<0){
								lost = 1;
							} else {
								lost++;
							}
						}
						if(bmhp<5){
							if(bmhpTimes>0){
								bmhpTimes = -1;
							} else {
								bmhpTimes--;
							}
						} else {
							if(bmhpTimes<0){
								bmhpTimes = 1;
							} else {
								bmhpTimes++;
							}
						}
						
						bmhpChartData.push(bmhp);
						lostChartData.push(lost);
						bmcpChartData.push(log.bwp);
						totalChartData.push(total.toFixed(2));
						bmhpTimesChartData.push(bmhpTimes);
					}
					setBmhpChart(bmhpChartData,lostChartData,bmcpChartData,xData,bmhpTimesChartData);
					setTotalChart(totalChartData,xData);
				}
			});
	}

	function anls(){
		var code = $("#code").val();
		init(code);
	}

	function changeCode(code){
		$("#code").val(code);
		anls();
	}

	function initAnlsData(){
		$.ajax({
			url:'/Anls/trade/getAnlsData'
			,dataType:'json'
			,success:function(data){
				if(!data) return false;
				anlsData = data;
				for(var code in data){
					var val = JSON.parse(data[code]);
					var html = "<input type='button' id='code_"+code+"' value='"+code+"' class='btn btn-default' onclick=changeCode('"+code+"') />";
					$("#codeList").append(html);	
				}
			}
		});
	}

	var curAutoCode = null;
	function autoView(){
		$.ajax({
			url:'/Anls/report/getChartAutoView'
			,dataType:'json'
			,success:function(data){
				if(!data) return false;
				if(data.code){
					if(curAutoCode == null || curAutoCode != data.code){
						$("#code").val(data.code);
						anls();
						curAutoCode = data.code;
					}
				}
			}
		});
		
		window.setTimeout(function(){
			autoView();
		},500);
	}
	
	$(function(){
		autoView();
		initAnlsData();
		anls();
		//init('000007');
		$("#prevBtn").click(function(){
			var curVal = $("#code").val();
			var prevVal = $("#searchCodeList").find("input[value="+curVal+"]").prev().val();
			
			$("#code").val(prevVal);
			anls();
		});

		$("#nextBtn").click(function(){
			var curVal = $("#code").val();
			var nextVal = $("#searchCodeList").find("input[value="+curVal+"]").next().val();
			$("#code").val(nextVal);
			anls();
		});
		
		$("#code").change(function(){
			var val = $(this).val()+'';
			var first = val.substring(0,1);
			var end = val.substring(1,val.length);
			var zero = "";
			for(var i=0;i<(5-end.length);i++){
				zero += "0";
			}
			val = first + zero + end;
			$(this).val(val);
		});
		$("#code").focus(function(){
			$(this).select();
		});

		$("#code").keyup(function(event){
			if(event.keyCode ==13){
				anls();
				$(this).select();
			}
		});
	})
	
	function dataSearch(){
		if(!anlsData)return false;
		var loseLimitVal = $("#loseLimit").val();
		var highWinLtVal = $("#bmhpLimit").val();
		$("#searchCodeList").html("");
		for(var code in anlsData){
			if(!anlsData[code])continue;
			var val = JSON.parse(anlsData[code]);
			var loseLimit = val.loseLimit.cur;
			var highWinLt = val.highWinLt.cur;
			if(loseLimit>loseLimitVal && highWinLt>highWinLtVal){
				//console.info(highWinLt+","+highWinLtVal+"|"+(highWinLt>highWinLtVal));
				$("#code_"+code).css("background-color","red");
				var html = "<input class='btn btn-default' type='button' id='search_code_"+code+"' value='"+code+"' onclick=changeCode('"+code+"') />";
				$("#searchCodeList").append(html);	
			}
			/*
			if(highWinLt>highWinLtVal && highWinLtVal != 0){
				$("#code_"+code).css("background-color","yellow");
				var html = "<input class='input_bg_yellow' type='button' id='search_code_"+code+"' value='"+code+"' onclick=changeCode('"+code+"') />";
				$("#searchCodeList").append(html);	
			}
			*/
			
		}
	}
	
    </script>
  </head>

  <body>
    <?php require APP_PATH_WEB_ANLS.'Report/common/nav.php';?>
    <div class="container-fluid">
      <div class="row">
        <?php require APP_PATH_WEB_ANLS.'Report/common/sidebar.php';?>
        
          	
           	<div class="divSearch form-inline">
		      	<input type="text" class="form-control" style="width:100px;" id="code" value="600094" />
		      	<input type="text" class="form-control" style="width:80px;" id="inputDate" value="2015" />
		      	<input type="button" class="btn btn-primary btn-default" id="btn" value="搜索" onclick="anls()" />
		      	<input type="button" class="btn btn-primary btn-default" id="prevBtn" value="prev"  />
		      	<input type="button" class="btn btn-primary btn-default" id="nextBtn" value="next"  />
		      	&nbsp;&nbsp;|&nbsp;&nbsp;连输>loseLimit：<input style="width:50px" class='form-control'  type="text" id="loseLimit" value="3" />
		      	&nbsp;&nbsp;|&nbsp;&nbsp;最高少于bmhp：<input style="width:50px" class='form-control'  type="text" id="bmhpLimit" value="3" />
		      	<input type="button" class="btn btn-primary btn-default" id="btn" value="数据搜索" onclick="dataSearch()" />
			</div>
			<div class="divTop">
          		<div class="panel panel-default  btnList searchCode" id="searchCodeList">
				</div>
				<div class="panel panel-default btnList"  id="codeList">
				</div>
			</div>
			<div class="divChart">
          		<div id="bmhp" style="width: 1150px;height:400px; margin-top:10px;"></div>
		    	<div id="total" style="width: 1150px;height:400px;"></div>
		    </div>
        </div>
      </div>
    </div>

  </body>
</html>
